import os
import yaml
from config.conf import YAML_DIR


class YamlTools:
    @classmethod
    def read_yaml_file(self,filename):
        filename=YAML_DIR+"\\"+filename
        with open(filename,'r',encoding="utf-8") as file:
            content = file.read()
            #转换yaml格式 为列表 或者字典
            result = yaml.load(content,Loader=yaml.FullLoader)
        return result
    @classmethod
    def write_yaml_file(self,filename,data):
        #创建一个yaml文件的存放目录 yaml_data/
        path = YAML_DIR
        if not os.path.exists(path):
            os.makedirs(path)
            print("目录已经创建")
        filename=YAML_DIR +"\\"+ filename
        with open(filename,'w',encoding="utf-8") as file:
            yaml.dump(data,file,allow_unicode=True)
if __name__ == '__main__':
    # testdata = [{"username": "admin", "password": "123456", "expect": "签退", "type": "正向"},
    #             {"username": "", "password": "123456", "expect": "登录失败，请检查您的成员名或密码是否填写正确。",
    #              "type": "反向"},
    #             {"username": "admin", "password": "", "expect": "登录失败，请检查您的成员名或密码是否填写正确。",
    #              "type": "反向"}]
    # YamlTools.write_yaml_file("test.yaml",testdata)
    # print(YamlTools.read_yaml_file("test.yaml"))
    testdata = [{"username": "zzl123", "realname": "赵云", "num": "2", "password1":"123456","password2":"123456","email":"1234567@qq.com","expect":"禁用成员列表","type": "正向"},
                {"username": "zzl123", "realname": "", "num": "2", "password1":"123456","password2":"123456","email":"1234567@qq.com","expect":"真实姓名不能为空。","type": "反向"},
                {"username": "zzl1111111111111111111111111111111111111", "realname": "赵云", "num": "2", "password1":"123456","password2":"123456","email":"1234567@qq.com","expect":"用户名长度应当不超过30，且不小于0。","type": "反向"},
                {"username": "zzl123", "realname": "赵云", "num": "2", "password1":"123456","password2":"12345","email":"1234567@qq.com","expect":"两次密码应当相等。","type": "反向"}]
    YamlTools.write_yaml_file("test1.yaml", testdata)